<?php

namespace app\api\model\plus\activity;

use app\common\model\plus\activity\Active as ActiveModel;
use app\common\service\product\BaseProductService;

/**
 * 限时活动模型
 */
class Active extends ActiveModel
{
    public function checkOrderTime($data)
    {
        $result = ['code' => 0];
        if ($data['start_time'] > time()) {
            $result = ['code' => 10, 10 => '该活动未开始'];
        }
        if ($data['end_time'] < time()) {
            $result = ['code' => 20, 20 => '该活动已结束'];
        }
        $now_start_time = strtotime(date('Y-m-d') . ' ' . $data['day_start_time']);
        $now_end_time = strtotime(date('Y-m-d') . ' ' . $data['day_end_time']);
        if ($now_start_time > time()) {
            $result = ['code' => 30, 30 => '该活动今天未开始'];
        }
        if ($now_end_time < time()) {
            $result = ['code' => 40, 40 => '该活动今天已结束'];
        }
        return $result;
    }

    /**
     * 取最近要结束的一条记录
     */
    public static function getActive()
    {
        return (new static())->where('start_time', '<', time())
            ->where('end_time', '>', time())
            ->where('status', '=', 1)
            ->where('is_delete', '=', 0)
            ->order(['sort' => 'asc', 'create_time' => 'asc'])
            ->find();
    }

    /**
     * 获取限时活动
     */
    public function activityList($iden)
    {
        return $this->where('end_time', '>', time())
//            ->where('start_time', '<=', time())
            ->when($iden !== '', function ($query) use ($iden) {
                $query->where('identifying', $iden);
            })
            ->where('status', '=', 1)
            ->where('is_delete', '=', 0)
            ->select();
    }

    /**
     * 获取限时商品
     * 6.10显示未开始
     */
    public static function getProductSecKillDetail($product_id)
    {
        $active = (new static())->alias('a')
            ->where('a.end_time', '>', time())
            ->where('a.status', '=', 1)
            ->where('a.is_delete', '=', 0)
            ->join('activity_product s', 's.activity_id=a.activity_id')
            ->where('product_id', '=', $product_id)->where('s.is_delete', '=', 0)->where('s.status', '=', 10)
            ->field('s.*')
            ->find();
        $detail = "";
        if ($active) {
            $detail = (new Product())->getSeckillDetail($active['activity_product_id']);
            $detail['active'] = self::detailWithTrans($active['activity_id']);
            $detail['specData'] = BaseProductService::getSpecData($detail['product']);
        }
        return $detail;
    }

    /**
     * @Exp    检测是否参加活动及优惠券
     * @Author Mr.Z
     * @Date   2024-05-15
     * @param  [type]     $product_id [商品id]
     * @param  integer    $is_coupon  [是否可用优惠卷：0否，1是]
     * @return [type]                 [description]
     */
    public static function checkProductInActive($product_id,$is_coupon=0){
        $active = (new static())->alias('a')
            ->where('a.end_time', '>', time())
            ->where('a.status', '=', 1)
            ->where('a.is_delete', '=', 0)
            ->where('a.is_coupon', '=', $is_coupon)
            ->join('activity_product s', 's.activity_id=a.activity_id')
            ->where('product_id', '=', $product_id)->where('s.is_delete', '=', 0)->where('s.status', '=', 10)
            ->value('s.activity_id');

        if($active){
            return true;
        }
        return false;
    }
}
